package com.smoed.common.utilities.hashing;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class Sha1 {

    private MessageDigest SHAAlgorithm;
    private static final String SHA1_DIGEST_CLASS_NAME = "SHA1";

    public Sha1() {
        try {
            SHAAlgorithm = MessageDigest.getInstance(SHA1_DIGEST_CLASS_NAME);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public String getSHAHashCode(String url) {
        SHAAlgorithm.update(url.getBytes());
        final StringBuffer hexString = new StringBuffer();

        for (byte bite : SHAAlgorithm.digest()) {
            final String hex = Integer.toHexString(0xFF & bite);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        SHAAlgorithm.reset();
        return hexString.toString();
    }
}
